<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>
    function requestImg(src) {
      return new Promise(function (resolve, reject) {
        let img = new Image(); // 创建图片对象
        img.src = src;
        img.onload = function () { // 图片加载完成
          clearTimeout(timer);
          resolve(img);
        }
      })
    }

    var timer
    function timeout() {
      return new Promise(function (resolve, reject) {
        timer = setTimeout(function () {
          console.log('图片加载超时');
          reject('图片加载超时')
        }, 30);
      })
    }

    Promise.race([requestImg('https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819_960_720.jpg'), timeout()])
      .then(function (res) {
        console.log(res);
        document.body.appendChild(res);
      })
      .catch(function (err) {
        console.log(err);
      });
  </script>
</body>

</html>